'use strict'
import React, {
    Component
} from 'react'
import {
    Link,
    hashHistory
} from 'react-router'

import Header from './Common/Header'
import Footer from './Common/Footer'
import {
    HttpService,
    getLocalTime,
    Tips
} from '../Tools'

export default class NtcDetail extends Component {
    constructor(props) {
        super(props);
        this.state = {
            noticeDetail: []
        }
        this.url = '/jxt-commonbackground/ajaxJybNotice.do'
        this.data = {
            jybTicket: sessionStorage.ticket,
            noticeId: this.props.params.id,
            withSelfSignLog: true
        }
    }
    componentWillMount() {
        this.data.method = 'm104ReadNotice'
        HttpService.query({
            url: this.url,
            data: this.data,
            //async: false,
            success: (res => {
                if (res.code == 0) {
                    this.setState({
                        noticeDetail: res.r
                    })
                    return;
                }
                Tips(res.msg, 0)
            })
        })
    }

    //附件预览
    linkDown(url) {
        if (sessionStorage.wherefrom == 'ios') {
            location.href = url;
        } else {
            hashHistory.push(url);
        }
    }
    //已读处理
    ntcRead(e) {
        this.data.method = 'm106SignReadNotice'
        this.data.userid = sessionStorage.userid
        HttpService.query({
            url: this.url,
            data: this.data,
            success: (res => {
                if (res.code == 0) {
                    let noticeDetail = this.state.noticeDetail
                    noticeDetail.signLog = [];
                    this.setState({
                        noticeDetail
                    })
                } else {
                    Tips(res.msg, 0);
                }
            })
        })
    }
    render() {
        const {
            title,
            createDate,
            content,
            files,
            fileCount,
            requireSign,
            signLog
        } = this.state.noticeDetail
        const fileTagList = files ? files.map((item, index) => {
            let link = ''
            if (sessionStorage.wherefrom == 'ios') {
                link = item.fileUrl + '&app=true&appdown=true&filename=' + encodeURIComponent(item.filename)
            } else {
                link = '/NtcDetailFile/' + this.props.params.id + '/' + encodeURIComponent(item.filename) + '/' + encodeURIComponent(item.fileUrl)
            }
            return (
                <li className={item.filename.split('.')[1].toLocaleLowerCase()+' ntcfile'} onClick={this.linkDown.bind(this,link)}>
                    <div>{item.filename}</div>
                    <p>{item.filesize}KB</p>
                </li>
            )
        }) : ''
        return (
            <div className='hasfoot2'>
                <Header title='通知详情'/>
                {files ?
                <div className="pjb_noticelist pjb_ntcdetail">
                    <div className="pnt_item">
                        <h5>{title}</h5>
                        <div className="time">
                            {getLocalTime(createDate)}
                        </div>
                        <div className="intro" dangerouslySetInnerHTML={{
                            __html: content
                        }}></div>
                    </div>
                    {fileCount == 0
                        ? ''
                        : <div className="pnt_other">
                            <h5>附件</h5>
                            <ul className="filelist">
                                {fileTagList}
                            </ul>
                        </div>}

                </div>:''}
                <div>
                    {requireSign == 1
                        ? <Footer>
                            {signLog
                                ?<a className='pjb_ntcread active'>我已阅读</a>
                                :<a className='pjb_ntcread' onClick={this.ntcRead.bind(this)}>我已阅读</a>
                                }
                            </Footer>
                        : ''}
                    {/*<div className="pjb_noticelisttch">
                        <div className="typebar">
                            <a className="active">已读</a>
                            <a>未读</a>
                        </div>
                        <div className="userlist">
                            <a>
                                <img src=''/>
                                <span>学生1的妈妈</span>
                            </a>
                        </div>
                    </div>*/}
                </div>
            </div>
        )
    }
}
